<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>系统监控</title>
    <!-- 引入样式库 -->
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.10.0/font/bootstrap-icons.css">
    <!-- echarts图表库 -->
    <script src="https://cdn.jsdelivr.net/npm/echarts@5.4.3/dist/echarts.min.js"></script>
    <style>
        .metric-card {
            border-radius: 10px;
            padding: 20px;
            margin-bottom: 20px;
            box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
            transition: transform 0.3s;
        }
        .metric-card:hover {
            transform: translateY(-5px);
        }
        .metric-value {
            font-size: 2rem;
            font-weight: bold;
        }
        .metric-title {
            color: #6c757d;
            font-size: 1rem;
        }
        .chart-container {
            height: 300px;
            width: 100%;
            margin-bottom: 20px;
        }
        .system-info-card {
            border-radius: 10px;
            padding: 20px;
            margin-bottom: 20px;
            box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
        }
        .system-info-item {
            margin-bottom: 10px;
        }
    </style>
</head>
<body>
    <div class="container-fluid mt-4">
        <h2 class="mb-4">系统监控</h2>
        
        <!-- 监控指标卡片 -->
        <div class="row">
            <div class="col-md-3">
                <div class="metric-card bg-primary bg-opacity-10">
                    <div class="metric-title">CPU使用率</div>
                    <div class="metric-value" id="cpu-percent">--</div>
                    <div class="progress mt-2">
                        <div class="progress-bar" id="cpu-progress" role="progressbar" style="width: 0%"></div>
                    </div>
                </div>
            </div>
            <div class="col-md-3">
                <div class="metric-card bg-success bg-opacity-10">
                    <div class="metric-title">内存使用率</div>
                    <div class="metric-value" id="memory-percent">--</div>
                    <div class="progress mt-2">
                        <div class="progress-bar bg-success" id="memory-progress" role="progressbar" style="width: 0%"></div>
                    </div>
                </div>
            </div>
            <div class="col-md-3">
                <div class="metric-card bg-warning bg-opacity-10">
                    <div class="metric-title">磁盘使用率</div>
                    <div class="metric-value" id="disk-percent">--</div>
                    <div class="progress mt-2">
                        <div class="progress-bar bg-warning" id="disk-progress" role="progressbar" style="width: 0%"></div>
                    </div>
                </div>
            </div>
            <div class="col-md-3">
                <div class="metric-card bg-info bg-opacity-10">
                    <div class="metric-title">系统运行时间</div>
                    <div class="metric-value" id="uptime">--</div>
                </div>
            </div>
        </div>
        
        <!-- 图表区域 -->
        <div class="row">
            <div class="col-md-6">
                <div class="card">
                    <div class="card-header">
                        CPU使用率历史
                    </div>
                    <div class="card-body">
                        <div id="cpu-chart" class="chart-container"></div>
                    </div>
                </div>
            </div>
            <div class="col-md-6">
                <div class="card">
                    <div class="card-header">
                        内存使用率历史
                    </div>
                    <div class="card-body">
                        <div id="memory-chart" class="chart-container"></div>
                    </div>
                </div>
            </div>
        </div>
        
        <!-- 系统信息区域 -->
        <div class="row mt-4">
            <div class="col-md-12">
                <div class="system-info-card">
                    <h4 class="mb-4">系统信息</h4>
                    <div class="row">
                        <div class="col-md-6">
                            <div class="system-info-item">
                                <strong>主机名：</strong>
                                <span id="hostname">--</span>
                            </div>
                            <div class="system-info-item">
                                <strong>操作系统：</strong>
                                <span id="os-name">--</span>
                            </div>
                            <div class="system-info-item">
                                <strong>系统版本：</strong>
                                <span id="os-version">--</span>
                            </div>
                        </div>
                        <div class="col-md-6">
                            <div class="system-info-item">
                                <strong>启动时间：</strong>
                                <span id="boot-time">--</span>
                            </div>
                            <div class="system-info-item">
                                <strong>CPU核心数：</strong>
                                <span id="cpu-count">--</span>
                            </div>
                            <div class="system-info-item">
                                <strong>内存总量：</strong>
                                <span id="memory-total">--</span>
                            </div>
                            <div class="system-info-item">
                                <strong>磁盘总量：</strong>
                                <span id="disk-total">--</span>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- 引入Bootstrap和jQuery -->
    <script src="https://cdn.jsdelivr.net/npm/jquery@3.6.0/dist/jquery.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
    <script>
        // 初始化图表
        const cpuChart = echarts.init(document.getElementById('cpu-chart'));
        const memoryChart = echarts.init(document.getElementById('memory-chart'));
        
        // 图表配置
        const cpuOption = {
            title: {
                text: 'CPU使用率',
                left: 'center'
            },
            tooltip: {
                trigger: 'axis',
                formatter: '{b}: {c}%'
            },
            xAxis: {
                type: 'category',
                data: [],
                boundaryGap: false,
                axisLabel: {
                    interval: 'auto',
                    rotate: 30
                }
            },
            yAxis: {
                type: 'value',
                min: 0,
                max: 100,
                interval: 10,
                axisLabel: {
                    formatter: '{value}%'
                }
            },
            series: [{
                name: 'CPU使用率',
                type: 'line',
                data: [],
                smooth: true,
                areaStyle: {
                    opacity: 0.3
                },
                itemStyle: {
                    color: '#0d6efd'
                }
            }]
        };
        
        const memoryOption = {
            title: {
                text: '内存使用率',
                left: 'center'
            },
            tooltip: {
                trigger: 'axis',
                formatter: '{b}: {c}%'
            },
            xAxis: {
                type: 'category',
                data: [],
                boundaryGap: false,
                axisLabel: {
                    interval: 'auto',
                    rotate: 30
                }
            },
            yAxis: {
                type: 'value',
                min: 0,
                max: 100,
                interval: 10,
                axisLabel: {
                    formatter: '{value}%'
                }
            },
            series: [{
                name: '内存使用率',
                type: 'line',
                data: [],
                smooth: true,
                areaStyle: {
                    opacity: 0.3
                },
                itemStyle: {
                    color: '#198754'
                }
            }]
        };
        
        cpuChart.setOption(cpuOption);
        memoryChart.setOption(memoryOption);
        
        // 获取监控数据并更新页面
        function fetchMonitorData() {
            $.ajax({
                url: '/admin/api/monitor',
                type: 'GET',
                headers: {
                    'Authorization': 'Bearer ' + localStorage.getItem('token')
                },
                dataType: 'json',
                success: function(data) {
                    // 更新系统信息
                    updateSystemInfo(data.system_info);
                    
                    // 更新资源使用情况
                    updateResourceUsage(data.resource_usage);
                    
                    // 更新图表数据
                    updateCharts(data.cpu_history, data.memory_history);
                },
                error: function(err) {
                    console.error('获取监控数据失败', err);
                    // 添加错误提示
                    if (err.status === 403) {
                        alert('无权访问系统监控');
                    } else if (err.status === 401) {
                        // 未登录或token过期，重定向到登录页
                        window.location.href = '/login';
                    }
                }
            });
        }
        
        // 更新系统信息
        function updateSystemInfo(systemInfo) {
            $('#hostname').text(systemInfo.hostname);
            $('#os-name').text(systemInfo.os_name);
            $('#os-version').text(systemInfo.os_version);
            $('#uptime').text(systemInfo.uptime);
            $('#boot-time').text(systemInfo.boot_time);
        }
        
        // 更新资源使用情况
        function updateResourceUsage(resourceUsage) {
            // CPU
            $('#cpu-percent').text(resourceUsage.cpu_percent.toFixed(1) + '%');
            $('#cpu-progress').css('width', resourceUsage.cpu_percent + '%');
            $('#cpu-count').text(resourceUsage.cpu_count);
            
            // 内存
            $('#memory-percent').text(resourceUsage.memory_percent.toFixed(1) + '%');
            $('#memory-progress').css('width', resourceUsage.memory_percent + '%');
            $('#memory-total').text(resourceUsage.memory_total);
            
            // 磁盘
            $('#disk-percent').text(resourceUsage.disk_percent.toFixed(1) + '%');
            $('#disk-progress').css('width', resourceUsage.disk_percent + '%');
            $('#disk-total').text(resourceUsage.disk_total);
        }
        
        // 更新图表数据
        function updateCharts(cpuHistory, memoryHistory) {
            // 提取时间和值
            const cpuTimes = cpuHistory.map(item => item.timestamp);
            const cpuValues = cpuHistory.map(item => item.value);
            
            const memoryTimes = memoryHistory.map(item => item.timestamp);
            const memoryValues = memoryHistory.map(item => item.value);
            
            // 更新CPU图表
            cpuOption.xAxis.data = cpuTimes;
            cpuOption.series[0].data = cpuValues;
            cpuChart.setOption(cpuOption);
            
            // 更新内存图表
            memoryOption.xAxis.data = memoryTimes;
            memoryOption.series[0].data = memoryValues;
            memoryChart.setOption(memoryOption);
        }
        
        // 页面加载完成后获取数据
        $(document).ready(function() {
            fetchMonitorData();
            
            // 每10秒刷新一次数据
            setInterval(fetchMonitorData, 10000);
            
            // 窗口大小变化时调整图表大小
            window.addEventListener('resize', function() {
                cpuChart.resize();
                memoryChart.resize();
            });
        });
    </script>
</body>
</html>